<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <!-- 新增分类区域  -->
        <el-col>
          <el-button type="primary" @click="showAddDialog">新增分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <tree-table
        :data="cateList"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        :columns="columns"
        border
        :show-row-hover="false"
      >
        <template slot="isok" scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color:lightgreen;"></i>
          <i class="el-icon-error" style="color:red;" v-else></i>
        </template>
        <template slot="order" scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 按钮 -->
        <template slot="opt" scope>
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
          </el-tooltip>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加分类按钮 -->
    <el-dialog title="新增分类" :visible.sync="addDialogVisible" width="50%" @close="addDialogClose">
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="选择父类:">
          <!--  options：级联菜单数据源
                props：控制级联菜单的属性
          v-model：绑定的数据，这个是选中的数据-->
          <el-cascader
            v-model="selerctedKeys"
            :options="ParentCateList"
            :props="caascaderProps"
            @change="handleCaderChange"
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        type: [1, 2, 3],
        pagenum: 1,
        pagesize: 5
      },
      // 商品分类数据
      cateList: [],
      // 父级分类列表
      ParentCateList: [],
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          // 将当前定义为模板列
          type: 'template',
          // 将当前定义为模板名称
          template: 'isok'
        },
        {
          label: '排序',
          // 将当前定义为模板列
          type: 'template',
          // 将当前定义为模板名称
          template: 'order'
        },
        {
          label: '操作',
          // 将当前定义为模板列
          type: 'template',
          // 将当前定义为模板名称
          template: 'opt'
        }
      ],
      total: 0,
      addDialogVisible: false,
      addCateForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 父级分类列表
      parentCateList: [],
      // 指定级连选择器
      caascaderProps: {
        expandTrigger: 'hover',
        checjStrictly: true,
        value: 'cat_id',
        label: 'cat_name'
      },
      // 选中父级分类的id数组
      selerctedKeys: []
    }
  },
  mounted () {
    // 商品分类数据
    this.getCateList()
  },
  methods: {
    // 获取商品分类数据
    async getCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败')
      }

      // 把数据给catelist
      this.cateList = res.data.result
      // 为总数据条数赋值
      this.total = res.data.total
    },
    handleSizeChange (val) {
      this.queryInfo.pagesize = val
      this.getCateList()
    },
    handleCurrentChange (val) {
      this.queryInfo.pagenum = val
      this.getCateList()
    },
    async getParentCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类失败')
      }
      this.ParentCateList = res.data
      console.log(res.data)
    },
    // 点击按钮、
    showAddDialog () {
      // 获取父级分类数据列表
      this.getParentCateList()
      // 展示出对话框
      this.addDialogVisible = true
    },
    handleCaderChange () {
      console.log(this.selerctedKeys)
    },
    addDialogClose () {
      console.log(this.$refs)
      this.$refs.addCateFormRef.resetFields()
      this.selerctedKeys = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },
    addCate () {
      this.$refs.addCateFormRef.validate(async (valid) => {
        if (!valid) return
        await this.$http.post('categories', this.addCateForm)
        this.$message({
          message: '添加成功',
          type: 'success',
          showClose: true
        })
        this.getCateList()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.zk-table {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
/deep/ .el-cascader-menu__wrap {
  height: 304px !important;
}
</style>
